package com.smartHire.controller.User.Order;

import com.alibaba.fastjson.JSONObject;
import com.smartHire.domain.Address;
import com.smartHire.domain.Goods;
import com.smartHire.domain.Shop;
import com.smartHire.service.GoodsService;
import com.smartHire.service.ShopService;
import com.smartHire.service.UserService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "ShowGoodServlet", value = "/ShowGoodServlet")
public class ShowGoodServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String good_id= request.getParameter("id");
        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        System.out.println("容器对象的信息========"+ctx);

        try {
            GoodsService goodsService = (GoodsService) ctx.getBean("goodsService");
            ShopService shopService = (ShopService) ctx.getBean("shopService");

            Goods goods = new Goods();
            goods.setGood_id(Integer.parseInt(good_id));

            goods = goodsService.queryGoodsByID(goods);

            Shop shop = new Shop();
            shop.setShop_id(goods.getShop_id());

            shop = shopService.queryShopById(shop);

            List list = new ArrayList();
            list.add(goods);
            list.add(shop);


            response.setCharacterEncoding("utf-8");
            String jsonStr = JSONObject.toJSONString(list);

            response.getWriter().write(jsonStr);


        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
